package com.swa99ylum.pxf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.swa99ylum.pxf.cell.CellFiller;
import com.swa99ylum.pxf.row.RowFiller;
import com.swa99ylum.pxf.sheet.SheetFiller;

public class Test3 {
	
public static void main(String[] args) throws Exception {
		
		
		File file = new File("test3.xlsx");
		System.out.println(file.getAbsolutePath());
		
		
		FileInputStream fis = new FileInputStream(file);
		XSSFWorkbook xwb = new XSSFWorkbook(fis);
		
		/** ===================================================== */
		
		/** 开发者 需要定义的 数据 */
		
		List<Student> sss = new ArrayList<>();
		sss.add(new Student(123, "张三", "上海", new Date(), "第一批"));
		sss.add(new Student(124, "李四", "北京", new Date(), "第一批"));
		sss.add(new Student(125, "王五", "四川", new Date(), "第二批"));
		sss.add(new Student(126, "赵六", "西安", new Date(), "第一批"));
		
		List<Student> sss2 = new ArrayList<>();
		sss2.add(new Student(441, "aaa", "上海", new Date(), "zzz"));
		sss2.add(new Student(442, "aaa123", "北京", new Date(), "zzxc"));
		sss2.add(new Student(443, "bbb", "四川", new Date(), "ttt"));
		sss2.add(new Student(444, "bbb456", "西安", new Date(), "qqq"));
		
		SheetFiller sheetFiller;
		sheetFiller = new SheetFiller(new String[]{"Sheet1", "aaa"}, new RowFiller[] {
				new RowFiller(1, sss, new CellFiller[] {
						new CellFiller(0, new Object[]{"@{batch}", "@{id}", "@{name}", "@{address}", "@{birthDay}"}),
						new CellFiller(8, new Object[]{"@{address}"}),
						new CellFiller(10, new Object[]{"@{birthDay}"}),
				}),
				new RowFiller(8, sss2, new CellFiller[] {
						new CellFiller(0, new Object[]{777, "信息", "@{id}", "@{name}", "@{address}", "@{birthDay}"}),
				})
		});
		sheetFiller = new SheetFiller(new String[]{"bbb"}, new RowFiller[] {
				new RowFiller(4, Arrays.asList(new Object[5]), new CellFiller[] {
						new CellFiller(0, new Object[]{"信息", "aaa", "111", 111, new Date(), 123555}),
				})
		});
//		sheetFiller = new SheetFiller(new String[]{"ccc1", "ccc2"}, new RowFiller[] {
//				new RowFiller(4, sss, new CellFiller[] {
//						//如果 找不到 属性，会报错
//						new CellFiller(0, new Object[]{777, "信息", "@{id123}", "@{name456}"}),
//				})
//		});
		
				
		/** ===================================================== */
		
		/** 模拟了 PluggableXSSFFiller 的 fill() 方法 */
		
		List<XSSFSheet> sheetList = new ArrayList<XSSFSheet>(); //待 填值
		
		String[] sheetNames = sheetFiller.getSheetNames(); //规定了 厚度，是下面 循环的 标准
		
		/*
		 * 设置 sheetFiller 的 sheets
		 */
		for(int i=0; i<sheetNames.length; i++) {
			
			//1、设置 rowList
			String sheetName = sheetNames[i];
			
			XSSFSheet sheet = xwb.getSheet(sheetName);
			if(sheet == null) sheet = xwb.createSheet(sheetName);
			sheetList.add(sheet);
		}
		
		XSSFSheet[] sheets = (XSSFSheet[]) sheetList.toArray(new XSSFSheet[0]);
		sheetFiller.setSheets(sheets);
		
		sheetFiller.fill();
		
		/** ===================================================== */
		
		FileOutputStream fo = new FileOutputStream(file);
		xwb.write(fo);
		fo.close();
		
	}


}
